 var utilwork=0;
function imagelistToObject(mulitImg,workid,fun) {
    var promiseAll = [],
        img = [],
        imgTotal = mulitImg.length;

    var _loop = function _loop(i) {
        promiseAll[i] = new Promise(function (resolve, reject) {
            img[i] = new Image();
            img[i].crossOrigin = "anonymous";
            img[i].src = mulitImg[i];
            img[i].id = mulitImg[i];
            img[i].onload = function () {
                //第i张加载完成
                resolve(img[i]);
               
                if( UtilProperty.background.backgroundimage.hasOwnProperty(img[i].id)){
                	if( UtilProperty.background.backgroundimage[img[i].id]==null){
                		 UtilProperty.background.backgroundimage[img[i].id] = img[i];
                	}
                }else{
                	UtilProperty[workid].imageObj[img[i].id] = img[i];
                }
            };
        });
    };

    for (var i = 0; i < imgTotal; i++) {
        _loop(i);
    }
    Promise.all(promiseAll).then(function (img) {
        //全部加载完成
        fun();
    });
   /*Promise.all(promiseAll).then(function (img) {
     //全部加载完成
        fun();
	}).catch(function(r){
	    console.log("err");
	    console.log(r);
	     //全部加载完成
        fun();
	});*/
   
}


//判断是否所有作品的封面的图片都加载完毕
function startcover(){
	idarr.splice(0,1);
 	utilwork++;
	if(utilwork==$(".workeritem .work_itemlist").length){
		if(isbackground==true){
			getCoverCanvas();	   		
			$(".worker_con").show();
		}else{
			var t=0;
			 var bgInterval=setInterval(function(){
			 		if(isbackground==true){
			 			clearInterval(bgInterval);
						getCoverCanvas();				   		
						$(".worker_con").show();
					}else{
						t+=100;
						if(t>60000){
							clearInterval(bgInterval);
							getCoverCanvas();	
							$(".worker_con").show();
							isbackground=true;
						}
					}
			 },100);
			
			 
		}
	    
	}else{		
		if(idarr.length>0){
			getreadyworker();
		}
	}
 	
 }
 //得到每个作品的封面
 function getCoverCanvas(){
 	for(var a in UtilProperty){
 		if(!(a=='layout_category'||a=='background')){
 		var tempList=UtilProperty[a];
 		var w_id=a;
 		var p_id=tempList.workerInfo.product_id;
 		UtilProperty[w_id].base64image=new Array;
 		if(tempList.worker instanceof Array&&tempList.worker.length>0){
	 			for(var j=0;j<tempList.worker.length; j++){
	 				var item=tempList.worker[j]; 
	 				if(item.page==1){			 			
						var canvas=document.createElement("canvas");						
						var context=canvas.getContext("2d");
						var w=boxwindthheight.width;
						var h=boxwindthheight.height;
						var scale;		
						if(item.width / item.height > w / h) {
							scale=w/item.width;
						} else {
							scale=h/item.height;
						}
						canvas.height=item.height*scale;
						canvas.width=item.width*scale;	
						canvas.setAttribute('style','width:'+canvas.width/Pixel_Ratio+'px; height:'+canvas.height/Pixel_Ratio+'px;');
						//给相应的作品添加背景图片
						layoutbackground(item,context,canvas.width,canvas.height,scale);	
						if(item.background_id==0){
							// lym 2017.11.20 修改
							//给相应的作品添加背景
							context.clearRect(0,0,canvas.width,canvas.height);
							context.rect(0,0,canvas.width,canvas.height);
							context.closePath();
							context.fillStyle = 'rgba(250,250,250,1)';
							context.fill();
							context.closePath();
							context.save();	
						}
						drawToCanvas(w_id,canvas,j,item,context,scale,false,false);	
						var obj={title:item.title,pageid:item.id,imgurl:canvas.toDataURL()};							
						UtilProperty[w_id].base64image.push(obj);	
						$('.work_itemlist[work_id="'+w_id+'"] .albumimg').html('<img src="'+obj.imgurl+'"/>');
						break;
					}
		 		}
 			}
 		}
 	}
 	$("#loadtoast").hide();
 }



//检测作品中所有的控件是否都有填充了图片或文字
function checkCoverwork(w_id,i){
	var worker=UtilProperty[w_id].worker;	
	var isfa=false;	
	//for(i=0; i<worker.length;i++){		
		var item=worker[i];				
		if(item.control_list.length>0){
		for(k=0; k<item.control_list.length;k++){
			var element=item.control_list[k];			
			if(element.control_id==1){				
				if(element.element_list instanceof Array&&element.element_list.length>0){						
					if(!element.element_list[0].image_url){					
						isfa=true
					     return isfa;
					}
				//console.log(i)		
				}else{
					isfa=true
					return isfa;
				}
				
			}else if(element.control_id==2){				
				if(element.element_list instanceof Array&&element.element_list.length>0){						
					if(element.element_list[0].content==''||!element.element_list[0].content){					
						isfa=true
					     return isfa;
					}
				//console.log(i)		
				}else{
					isfa=true
					return isfa;
				}
				
			}
		
			
		}
	}else{
		isfa=true
		return isfa;
	}
	//}
	
	
	
}





//把当前的图片对象放置到相应的图片控件当中

function placeImageObj(w_id){
	var mulitImg=new Array;			
	var worker=UtilProperty[w_id].worker;
	
	var month=dataObj.month;
	var year=dataObj.year;
	var isdate=false;
	for(i=0; i<worker.length;i++){
		var item=worker[i];	
		if(UtilProperty[w_id].worker[i].background.ischange==undefined){
			UtilProperty[w_id].worker[i].background.ischange=false;		
		}
		if(item.background_id!=0){// lym 2018.01.02 修改 图片加载 原先的的id标志转为url标志	
			if(item.background.image_url.split('thumbnail/')[1]==''){
				var bgurl=item.background.image_url+bgsize_str;
				UtilProperty[w_id].worker[i].background.image_url=bgurl;
				if(UtilProperty.background.backgroundimage[bgurl]==undefined){
					mulitImg.push(bgurl);
					UtilProperty.background.backgroundimage[bgurl]=null;
				}	
			}else{					
				if(UtilProperty.background.backgroundimage[item.background.image_url]==undefined){
					mulitImg.push(item.background.image_url);
					UtilProperty.background.backgroundimage[item.background.image_url]=null;
				}	
			}
				
		}		
		for(k=0; k<item.control_list.length;k++){
			if(UtilProperty[w_id].worker[i].control_list[k].ischange==undefined){
				UtilProperty[w_id].worker[i].control_list[k].ischange=false;
			}
			
			var element=item.control_list[k];			
			if(element.control_id==1){				
				if(element.element_list instanceof Array&&element.element_list.length>0){	
					var list=element.element_list[0];					
					/*if(list.image_url!=""&&UtilProperty[w_id].imageObj[list.image_id]){						
					}else{						
						var imgurl=list.image_url+imgw+'x'+imgw;
						mulitImg.push(imgurl);			
						MoreImgid.push(list.image_id);
					}
					*/
					if(list.image_url!=""){
						if(list.image_url.split('thumbnail/')[1]==''){
							var img_url=list.image_url+imgsize_str;
							UtilProperty[w_id].worker[i].control_list[k].element_list[0].image_url=img_url;
							if(!UtilProperty[w_id].imageObj[img_url]){												
								if($.inArray(img_url, mulitImg)==-1){
									mulitImg.push(img_url);										
									isfla=true;
								}
							}
							
						}else{
							
							if(!UtilProperty[w_id].imageObj.hasOwnProperty(list.image_url)){								
								if($.inArray(list.image_url, mulitImg)==-1){
									mulitImg.push(list.image_url);										
									isfla=true;
								}
							}	
							
						}
					}
					
						
				}
			
			}else if(element.control_id==2){	
				if(element.element_list instanceof Array&&element.element_list.length>0){	
					UtilProperty[w_id].worker[i].control_list[k].element_list[0].content=$.trim(element.element_list[0].content);
					var a=JSON.parse(element.element_list[0].style);					
					if(typeof(a.format) != "undefined"){
					if(a.format!=""){	
						if(opentype==2||UtilProperty[w_id].worker[i].control_list[k].element_list[0].content==''){
						if(a.format=="YYYY"){
							UtilProperty[w_id].worker[i].control_list[k].element_list[0].content=year;
						}else if(a.format=="YY"){
							UtilProperty[w_id].worker[i].control_list[k].element_list[0].content=year.substring(-1,2)+'年';
						}else if(a.format=="MM"){
							UtilProperty[w_id].worker[i].control_list[k].element_list[0].content=month;
						}
						UtilProperty[w_id].worker[i].control_list[k].ischange=true;
						}
					}
			}	
					
				}
			}else if(element.control_id==3){//lym 2017 11.17 新增月历修改
				if(element.element_list.length > 0) {
					var dateList = element.element_list[0];
					if(dateList.style != "" && dateList.style != undefined) {
						var a = JSON.parse(dateList.style);
						var fontstyle = {
							align: a.align,
							color: a.color,
							direction: a.direction,
							line_space: a.line_space,
							max: a.max,
							size: a.size,
							format: a.format
						};
						isdate=true;
						if(opentype==2||UtilProperty[w_id].worker[i].control_list[k].element_list[0].content==''){
							UtilProperty[w_id].worker[i].control_list[k].element_list[0].content=year+'-'+month;
							UtilProperty[w_id].worker[i].control_list[k].ischange=true;
							UtilProperty[w_id].worker[i].control_list[k].element_list[0].date_list = calendarMonthdata(year, month, a.column, a.weekstyle, element.width, element.height, fontstyle);
							
						}else{
							UtilProperty[w_id].worker[i].control_list[k].element_list[0].date_list = calendarMonthdata(dateList.content.split('-')[0], dateList.content.split('-')[1], a.column, a.weekstyle, element.width, element.height, fontstyle);	
						}
					}
				}
			}
		
			
		}
		
		//lym 2017 11.27  新增月历修改	
		if(isdate){
		month=Number(month);
		year=Number(year);	
		year=month<12?year.toString():(year+1).toString();
		month=month<12?(month+1<10?'0'+(month+1).toString():(month+1).toString()):'01';
		isdate=false;
		}
	}
	
	imagelistToObject(mulitImg,w_id,function(){
		//打开编辑作品页面并且合成相应的页面
		
		if(UtilProperty[w_id].image_list){
				//遍历图片
				image_list();
		}else{
			//获取作品图片列表		
			image_listajax();
		}		
		startgetcanvas();
	})
	
}
//打开已经生成的作品
function openwork(){
	//生成作品
	var ele=$(".workeritem .onfoucs");
	$(".worker_con").hide();
	if(ele.attr("status") == 0) {
		document.title = "编辑作品";
		$(".originalwork").show();
		creatPage(work_id);
		console.log(1)
	}else{
		// 已提交的作品
		/*document.title='提交作品';		
		$(".submitalbum ").show();*/
		//预览已经提交的作品
		if(UtilProperty[work_id].base64image.length!=UtilProperty[work_id].worker.length){
			creatPage(work_id);
			getpreviewPic();
			
		}else{
			getpreviewPic();
		}
		
		
		//history.pushState({page: 'previewcon'}, '预览', '');
	}
	$("#loadtoast").hide();
	opentype=0;
}
//打开新建的作品
function openewwork(){
	creatPage(work_id);
	changeoriginalcanvas(0);
	$(".worker_con").hide();
	$(".originalwork").show();
	document.title = "编辑作品";
	$("#loadtoast").hide();
	opentype=0;
	
}
//打开编辑作品页面并且合成相应的页面
function startgetcanvas(){
	//打开已经生成的作品	
	if(opentype==1){
		openwork();
	}else if(opentype==2){
		openewwork()
	}
	
}
